﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OAclient.Models;
using ServiceApplication.Service.Basic;
using CommonLibrary.DataModel.Basic;
using System.Data;
using System.Web.Caching;
using OAclient.Filters;

namespace OAclient.Controllers
{
    [VaildateLogin]
    [ExceptionFilter]
    [LoggerFilter]
    public class CalenderController : Controller
    {
        //
        // GET: /Calender/
        BasicService basicService = new BasicService();

        public ActionResult GetOutstandDate(String year, String month, Int32 dayCount, UserModel user)
        {
            var date = basicService.GetOutstandDate(year, month, dayCount, user.user.user_id.ToString());
            if (String.IsNullOrEmpty(date))
                date = "FAIL";
            return Content(date);
        }

        //public void AccessNoteByCurrDay(UserModel user)
        //{

        //    //if (HttpContext.Cache["notes"] == null)
        //    //    HttpContext.Cache.Add("notes", notes, null, DateTime.MaxValue, new TimeSpan(0, 1, 0, 0), CacheItemPriority.Default, null);
        //    //else
        //    //    HttpContext.Cache["notes"] = notes;
        //}

        //[OutputCache(Duration = 6000000, VaryByParam = "type")]
        public ActionResult GetTodayNote(UserModel user)
        {
            var dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("text");
            dt.Columns.Add("time");

            var date = DateTime.Now.ToString("yyyy-MM-dd");
            var notes = basicService.GetNotesUtilCurrDay(date, user.user.user_id.ToString());
            if (notes != null)
            {
                var rowCount = notes.Rows.Count;

                for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
                {
                    //var timeData = DateTime.Parse(notes.Rows[rowIndex]["msg_time"].ToString());
                    //var timeNow = DateTime.Now;
                    //if (timeNow.AddMinutes(10).CompareTo(timeData) >= 0)
                    //{
                    var newRow = dt.NewRow();
                    var note_text = notes.Rows[rowIndex]["note_text"].ToString();
                    if (!String.IsNullOrEmpty(note_text) & note_text.Length > 10)
                        note_text = note_text.Substring(0, 10) + "...";
                    newRow[0] = notes.Rows[rowIndex]["id"];
                    newRow[1] = note_text;
                    newRow[2] = notes.Rows[rowIndex]["msg_time"].ToString();
                    dt.Rows.Add(newRow);
                    //}
                }
                if (dt.Rows.Count == 0)
                    return null;
            }
            else
                return null;
            ViewData["msgdt"] = dt;

            return PartialView();
        }


        public void ConfirmNote(String noteId, UserModel user)
        {
            basicService.ReadNote(noteId);
        }

        public ActionResult ShowNote(String date, UserModel user)
        {
            var notes = basicService.GetNotes(date, user.user.user_id.ToString());
            ViewData["date"] = date;
            ViewData["notes"] = notes;
            ViewData["slcthour"] = produce_hour_slct();
            ViewData["slctminu"] = produce_minu_slct();
            return View("AddNotes");
        }

        public ActionResult DoNote(UserModel user)
        {
            var id = Request.Form["noteid"];
            var text = Request.Form["operNote"];
            var date = Request.Form["date"];
            var textMsg = Request.Form["textMsg"];
            var noteType = Request.Form["noteType"];

            if (String.IsNullOrEmpty(text))
                return Content("EMPTY");

            var hour = Request.Form["slcthour"];
            var minu = Request.Form["slctminu"];

            var msgTime = date;
            msgTime += " " + hour + ":" + minu + ":0";

            if (textMsg.Contains("true"))
                textMsg = "True";

            if (noteType.Contains("true"))
                noteType = "HURRY";
            else
                noteType = "NORMAL";

            CledEntity cledEntity = new CledEntity
            {
                id = id,
                noteText = text,
                noteTime = date,
                textMsg = Convert.ToBoolean(textMsg),
                msgTime = msgTime,
                noteType = noteType
            };
            if (String.IsNullOrEmpty(id))
                basicService.DoNote(cledEntity, user.user.user_id.ToString());
            else
                basicService.UpdateNote(cledEntity);
            //this.AccessNoteByCurrDay(user);
            return RedirectToAction("ShowNote");
        }

        public ActionResult DeleteNote(String noteid, String date, UserModel user)
        {
            basicService.DeleteNote(noteid);
            //this.AccessNoteByCurrDay(user);
            return RedirectToAction("ShowNote");
        }

        public ActionResult NodeMrg(UserModel user)
        {
            return Content("从新构建中");
        }

        private List<SelectListItem> produce_hour_slct()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            for (int i = 0; i < 24; i++)
            {
                items.Add(new SelectListItem
                {
                    Text = i.ToString(),
                    Value = i.ToString()
                });
            }
            return items;
        }

        private List<SelectListItem> produce_minu_slct()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            for (int i = 0; i < 59; i++)
            {
                items.Add(new SelectListItem
                {
                    Text = i.ToString(),
                    Value = i.ToString()
                });
            }
            return items;
        }
    }
}
